<template>
  <div class="app-container">
    <!-- 顶部搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="75px">
      <el-form-item label="立项年度" prop="year">
        <el-select v-model="queryParams.year" placeholder="请选择立项年度" clearable>
          <el-option
            v-for="dict in dict.type.year"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="合同名称" prop="htName">
        <el-input
          v-model="queryParams.htName"
          placeholder="请输入合同名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同状态:" prop="processStatus">
        <el-select v-model="queryParams.processStatus" placeholder="请选择合同状态" clearable size="mini">
          <el-option
            v-for="dict in dict.type.process_business_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="合同编号" prop="htBh">
        <el-input
          v-model="queryParams.htBh"
          placeholder="请输入合同编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="承担单位" prop="cdDw">
        <el-input
          v-model="queryParams.cdDw"
          placeholder="请输入承担单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 顶部按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['htbz:htbz:export']"
        >导出Excel
        </el-button>
      </el-col>
      <el-col :span="1.5">
      <el-button
        size="mini"
        type="primary"
        icon="el-icon-delete"
        @click="handleExports"
      >导出docx</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table @row-click="clickRow" ref="table" highlight-current-row
              v-loading="loading" :data="htbzList" @selection-change="handleSelectionChange"
              :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="序号" align="center" type="index" width="50"/>
      <el-table-column label="年度" align="center" prop="year" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.year" :value="scope.row.year"/>
        </template>
      </el-table-column>
      <el-table-column label="合同编号" align="center" prop="htBh" show-overflow-tooltip/>
      <el-table-column label="合同名称" align="center" prop="htName" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleButtonClick(scope.row)"
          >
            <el-link type="primary">{{ scope.row.htName }}</el-link>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="课题归属" align="center" prop="ktGs" width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.kylx_ktgs" :value="scope.row.ktGs"/>
        </template>
      </el-table-column>
      <el-table-column label="承担单位" align="center" prop="cdDw" show-overflow-tooltip/>
      <el-table-column label="负责人" align="center" prop="fzName"/>
      <el-table-column label="合同经费" align="center" prop="xmZjf" width="130"/>
      <el-table-column label="甲方资助" align="center" prop="jfZf"/> <!--甲方支付(万元)-->
      <!--      <el-table-column label="关联项目" align="center" prop="glXm"/>-->
      <!--      <el-table-column label="项目申报ID" align="center" prop="xmsbId"/>-->
      <el-table-column label="起时间" align="center" prop="qTime" width="130">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.qTime, '{y}-{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="止时间" align="center" prop="zTime" width="130">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.zTime, '{y}-{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目延期" align="center" prop="yqCount" width="100">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleXmYqCk(scope.row)"
          >
            <el-link v-if="scope.row.yqCount != 0" type="primary">{{ scope.row.yqCount }}</el-link>
          </el-button>
          <el-link type="danger" style="color: #0D1623" disabled v-if="scope.row.yqCount == 0">{{ '无' }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="变更次数" align="center" prop="htBgCount" width="100">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleHtBgCk(scope.row)"
          >
            <el-link v-if="scope.row.htBgCount != 0" type="primary">{{ scope.row.htBgCount }}</el-link>
          </el-button>
          <el-link type="danger" style="color: #0D1623" disabled v-if="scope.row.htBgCount == 0">{{ '无' }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="processStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.process_business_status" :value="scope.row.processStatus" />
        </template>
      </el-table-column>
      <el-table-column label="录入人员" align="center" prop="lrRy"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleJinDu(scope.row)"
            v-if="scope.row.processStatus !== 1"
          >查询进度</el-button>
          <el-link type="danger" disabled v-else>{{ '未提交审批' }}</el-link>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查询进度   -->
    <Jindu ref="picture" />

    <!-- 合同详情 -->
    <HtXq ref="htXq"/>

    <!-- 项目延期查看 -->
    <XmYqCk ref="xmYqCk"/>

    <!-- 合同变更查看 -->
    <HtBgCk ref="htBgCk"/>

  </div>
</template>

<script>
import {
  exportWord,
  listHtbz,
} from "@/api/xmgl/htgl/htbz/htbz";
import {getToken} from "@/utils/auth";
import HtDr from "@/views/xmgl/htgl/htbz/components/HtDr";
import HtXzKt from "@/views/components/ktXz/HtXzKt";
import Jindu from "@/views/components/liuChengState/jindu";
import HtXq from "@/views/xmgl/htgl/htbz/components/HtXq";
import XmYqCk from "@/views/xmgl/htgl/htbz/components/XmYqCk";
import HtBgCk from "@/views/xmgl/htgl/htbz/components/HtBgCk";

export default {
  name: "htcx",
  components: {HtBgCk, XmYqCk, HtXq, Jindu, HtXzKt, HtDr},
  dicts: ['htbz_mj', 'rcpt_zc', 'kylx_zcry_ktfg', 'year', 'kylx_ktgs', 'process_business_status'],
  data() {
    return {
      selectedRow: null, // 保存当前选中的行
      // 年度计划子表选中数据
      checkedHtglHtbzNdjh: [],
      // 预算经费子表选中数据
      checkedHtglHtbzYsjf: [],
      // 支付计划子表选中数据
      checkedHtglHtbzZfjh: [],
      // 课题承担人员子表选中数据
      checkedHtglHtbzCdry: [],
      activeName: 'htJbzl', //默认打开的标签
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedHtglHtbzFj: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同编制表格数据
      htbzList: [],
      // 合同附件表格数据
      htglHtbzFjList: [],
      // 年度计划和考核目标表格数据
      htglHtbzNdjhList: [],
      // 项目预算经费表格数据
      htglHtbzYsjfList: [],
      // 项目经费支付计划表格数据
      htglHtbzZfjhList: [],
      // 课题承担人员表格数据
      htglHtbzCdryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        htName: null,
        year: null,
        ktGs: null,
        glXm: null,
        xmsbId: null,
        processStatus: null,
        htBh: null,
        cdDw: null,
        userId: null,
        deptId: null
      },
      getHtBzId: null, //从后端获取的合同编制主键ID (UUID)
      fjUpload: false, //合同附件上传弹窗
      xg: false, //判断是新增还是修改 (false新增 true修改)
      allFilesUploaded: false, // 添加这个来追踪所有文件是否上传完成
      //要修改的 科技人员id
      htBzIdEdit: "",
      uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传文件服务器地址
      fileList: [],
      headers: {Authorization: "Bearer " + getToken()},
      number: 0,
      uploadList: [],
      fileType: ["doc", "xls", "ppt", "txt", "pdf", "docx", "zip"],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //点击变更次数打开合同变更查看弹窗
    handleHtBgCk(row) {
      this.$refs.htBgCk.handleOpen(row);
    },
    //点击项目延期打开项目延期查看弹窗
    handleXmYqCk(row) {
      this.$refs.xmYqCk.handleOpen(row);
    },
    //导出按钮操作
    handleExports() {
      const row = this.selectedRow; // 获取当前选中的行
      const queryParams = this.queryParams;
      const htbzId = row.htbzId;
      this.$confirm('是否确认导出word文档?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true
        return exportWord(queryParams, htbzId); // Passing userId to the exportWord function
      }).then(response => {
        // console.log('/profile/upload/' + response.msg)
        this.$download.resource('/profile/upload/' + response.msg);
        this.loading = false
      }).catch(() => {});
    },
    // 查询详情信息
    handleButtonClick(row) {
      this.$refs.htXq.handleOpen(row);
    },
    //查询进度
    handleJinDu(row) {
      this.$refs.picture.handleOpen(row);
    },
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      const selectedRows = table.selection;

      // 否则取消之前选中的行，并选中当前点击的行
      selectedRows.forEach(selectedRow => {
        table.toggleRowSelection(selectedRow);
      });
      table.toggleRowSelection(row);
    },
    /** 查询合同编制列表 */
    getList() {
      this.loading = true;
      listHtbz(this.queryParams).then(response => {
        console.log(response, 'dsaaaaaaaaa')
        this.htbzList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      let ids = selection.map(item => item.htbzId)
      if (ids.length === 1) {
        this.ids = ids[0]
      } else {
        this.ids = ids
      }
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('htbz/htbz/export', {
        ...this.queryParams
      }, `htbz_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
